////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRay_ExplodingBarrel extends UTSimpleDestroyable
                     placeable;

var() int Hits;
var() StaticMesh MeshOnDamaged;
var() SoundCue SoundOnDamaged;
var ParticleSystemComponent Explosion;

simulated function PostBeginPlay()
{
	Super.PostBeginPlay();
    SetCollision(true, true);
}

simulated function RemoveMesh()
{
	if(MeshOnDestroy != None)
	{
		StaticMeshComponent.SetStaticMesh(MeshOnDestroy);
	}
	else
	{
		StaticMeshComponent.SetStaticMesh(None);
		DetachComponent(StaticMeshComponent);
	}
}

simulated function TakeDamage(int DamageAmount, Controller EventInstigator, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser)
{
   if(Hits == 0)
   {
      Hits -= 1;
      GoBoom();
   }
   if(Hits >= 1)
   {
      Hits -= 1;
      if(MeshOnDamaged != None)
  	  {
         StaticMeshComponent.SetStaticMesh(MeshOnDamaged);
      }
	  if(SoundOnDamaged != None)
  	  {
  	     SoundOnDamaged.VolumeMultiplier = 1.5;
         PlaySound(SoundOnDamaged, true);
      }

   }
}

simulated function GoBoom()
{
	// Fire particles
	if(ParticlesOnDestroy != None)
	{
		Explosion = WorldInfo.MyEmitterPool.SpawnEmitter(ParticlesOnDestroy, Location, Rotation);
		Explosion.SetScale(1);
	}

	// Play sound
	if(SoundOnDestroy != None)
	{
	    SoundOnDestroy.VolumeMultiplier = 0.9;
		PlaySound(SoundOnDestroy, true);
	}
	HurtRadius(5, 250, Class'BailterRayDmgType_Flame', 50000, self.Location,,, true);
	HurtRadius(5, 250, Class'BailterRayDmgType_Flame', 50000, self.Location,,, true);

	bDestroyed = true;
	SetTimer(0.2, true, 'RemoveMesh');
	Self.Destroy();
}

event Detach( Actor Other )
{

}

simulated function bool HurtRadius
(
	float				BaseDamage,
	float				DamageRadius,
	class<DamageType>	DamageType,
	float				Momentum,
	vector				HurtOrigin,
	optional Actor		IgnoredActor,
	optional Controller InstigatedByController = Instigator != None ? Instigator.Controller : None,
	optional bool       bDoFullDamage
)
{
	local Actor	Victim;
	local bool bCausedDamage;
	local UTPawn P;

	// Prevent HurtRadius() from being reentrant.
	if ( bHurtEntry )
		return false;

	bHurtEntry = true;
	bCausedDamage = false;
	foreach VisibleCollidingActors( class'Actor', Victim, DamageRadius, HurtOrigin )
	{
		if ( !Victim.bWorldGeometry && (Victim != self) && (Victim != IgnoredActor) && (Victim.bProjTarget || (NavigationPoint(Victim) == None)) )
		{
			Victim.TakeRadiusDamage(InstigatedByController, BaseDamage, DamageRadius, DamageType, Momentum, HurtOrigin, bDoFullDamage, self);
			bCausedDamage = bCausedDamage || Victim.bProjTarget;
			if(Victim.IsPlayerOwned()){
			   P = UTPawn(Victim);
			   P.bCanRagDoll = true;
			   P.ForceRagdoll();
			   P.bCanRagDoll = false;
			}
		}
	}
	bHurtEntry = false;
	return bCausedDamage;
}

defaultproperties
{

   Hits = 1
   RespawnTime=0
   bDestroyOnPlayerTouch=False
   MeshOnDestroy=None
   MeshOnDamaged=StaticMesh'BailterRayAssetPackage.Meshes.RobotBarreldamaged'
   SoundOnDamaged=SoundCue'A_Weapon_ShockRifle.Cue.A_Weapon_SR_FireCue'
   SoundOnDestroy=SoundCue'A_Vehicle_Goliath.SoundCues.A_Vehicle_Goliath_Explode'
   ParticlesOnDestroy=ParticleSystem'VH_Raptor.Effects.P_Raptor_RocketExplosion'

   SpawnPhysMesh=None

   Begin Object class=StaticMeshComponent Name=Barrel ObjName=Barrel Archetype=StaticMeshComponent'BailterRay.Default__BailterRay_ExplodingBarrel'
      StaticMesh=StaticMesh'BailterRayAssetPackage.Meshes.RobotBarrel'
   End Object
   StaticMeshComponent=Barrel

   Components(1)=Barrel
   CollisionComponent=Barrel
   CollisionType=COLLIDE_BlockAll

   name="Default__BailterRay_ExplodingBarrel"
   ObjectArchetype=UTSimpleDestroyable'UTGame.Default__UTSimpleDestroyable'

}